import SwiftUI

import SwiftUI

struct AnimatableAttributes_Anchor : View {
    @State var isOpen: Bool = false
    
    var body: some View {
        VStack(spacing: 30) {
            Text("Animatable Attributes")
                .font(.largeTitle)
            
            Text("Rotation around anchor")
                .font(.title)
                .foregroundColor(.gray)
            
            Text("You can rotate around an anchor of your choosing. The default is rotation around the center point.")
                .font(.title3)
                .padding()
                .frame(maxWidth: .infinity)
                .background(
                    Color.indigo
                )
            
            makeDemoView()
            
            Spacer()
            
            Button(action: {
                isOpen.toggle()
            }, label: {
                Text("Rotate clockwise")
            })
            .padding(.bottom, 50)
            .font(.title)
        }
    }
    
    func makeDemoView() -> some View {
        VStack(spacing: 20) {
            ZStack {
                RoundedRectangle(cornerRadius: 12)
                    .frame(width: 300, height: 150)
                    .foregroundColor(.pink)
                Text("Rotate from top left")   
                    .font(.title3)
            }
            .rotationEffect(Angle(degrees: isOpen ? 220.0 : 0), anchor: .topLeading)
            .animation(.easeInOut, value: isOpen)
            
            ZStack {
                RoundedRectangle(cornerRadius: 12)
                    .frame(width: 300, height: 150)
                    .foregroundColor(.blue)

                Text("Rotate from bottom right")                
            }
            .rotationEffect(Angle(degrees: isOpen ? 220.0 : 0), anchor: .bottomTrailing)
            .animation(.easeInOut, value: isOpen)
            
        }
        .padding(.top, 200)
        
    }
}
